using System;
using System.Linq;
using Caliburn.Micro;
using MediaOwl.Common.Busy;
using MediaOwl.Common.Dialog;

namespace MediaOwl.Common.ScreenManagement
{
    public class ExtendedConductor<T> : Conductor<T>.Collection.OneActive, IDialogCapable, IBusyCapable, IScreenExtensions
    {
        private bool initialLoading;

        #region Constructor
        public ExtendedConductor()
        {
            initialLoading = true;

            DialogConductor = new Conductor<IDialog>.Collection.OneActive();
            ((IActivate)DialogConductor).Activate();

            DialogConductor.Items.CollectionChanged += (s, e) =>
            {
                if (ActiveDialog == null)
                {
                    NotifyOfPropertyChange(() => ActiveDialog);
                }

                ActivateQueuedDialog();
            };
        }
        #endregion

        #region Implementation of IDialogCapable

        public bool HasActiveDialog { get { return ActiveDialog != null; } }

        public IDialog ActiveDialog { get { return DialogConductor.ActiveItem; } }

        public Conductor<IDialog>.Collection.OneActive DialogConductor { get; private set; }

        public void ShowDialog(IDialog dialog)
        {
            if (dialog == null || DialogConductor.Items.Contains(dialog))
                return;

            DialogConductor.Items.Add(dialog);

            if (HasActiveDialog)
                return;

            ActivateQueuedDialog();
        }

        private void ActivateQueuedDialog()
        {
            if (DialogConductor.Items.Count == 0) return;

            var dialog = DialogConductor.Items.FirstOrDefault(x => x.DialogMessageResult == DialogResult.None);

            if (dialog == null) return;

            DialogConductor.ActivateItem(dialog);
            NotifyOfPropertyChange(() => ActiveDialog);
            //DialogConductor.ActiveItem.Completed += (s, e) => ActivateQueuedDialog();
        }

        #endregion

        #region Implementation of IBusyCapable

        public bool IsBusy
        {
            get
            {
                return CurrentBusyViewModel != null;
            }
        }

        private IBusy currentBusyViewModel;
        public IBusy CurrentBusyViewModel
        {
            get { return currentBusyViewModel; }
            private set
            {
                if (currentBusyViewModel == value)
                    return;

                currentBusyViewModel = value;
                NotifyOfPropertyChange(() => CurrentBusyViewModel);
            }
        }

        public void ShowBusy(IBusy busyViewModel)
        {
            CurrentBusyViewModel = busyViewModel;
        }

        public void HideBusy()
        {
            CurrentBusyViewModel = null;
        }

        #endregion

        #region Implementation of IScreenExtensions

        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);
            if (initialLoading && ViewInitiallyLoaded != null)
                ViewInitiallyLoaded(view);
            initialLoading = false;
        }

        public Action<object> ViewInitiallyLoaded { get; set; }

        #endregion
    }
}